# Load the rpart library
library(rpart)
# Open three graphics windows
graphics.off(); windows(); windows(); windows()
# Read in the data
data<-read.table('src/dirty/stroke.csv',sep=",",header=TRUE)
names(data)
# Make factors of categorical variables and add labels
l<-vector(length=2)
l[1]="Stayer"
l[2]="Dropout"
data$status<-factor(data$STATUS,labels=l)
l<-vector(length=2)
l[1]="Male"
l[2]="Female"
data$gender<-factor(data$GENDER,labels=l)
## Set defaults
# 2-to-1 cost matrix
clmat<-matrix(nrow=2,ncol=2)
clmat[1,1]<-0; clmat[2,1]<-1; clmat[1,2]<-2; clmat[2,2]<-0;
# bucket and node size
my.control<-c(minsplit=10,minbucket=5,cp=0.001,maxcompete=2,maxsurrogate=1,
		usesurrogate=2,xval=10,surrogatestyle=0,maxdepth=4)
# Run classification tree
tree<-rpart(status~gender+DFIMBATH+NOCOMORB,data=data,method="class",control=my.control,parms=list(loss=clmat))
# Low-resolution plot
dev.set(which=1); plot(tree); text(tree)
# High-resolution plot
dev.set(which=2); post(tree,filename="")
# Cross-validation plots
dev.set(which=3); par(mfcol=c(2,1)); rsq.rpart(tree); par(mfcol=c(1,1))
# Tree pruning
pruned<-prune(tree,cp=0.0351)
dev.set(which=1); plot(pruned); text(pruned)